﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;
using Enterprise.Management.Data;

namespace Enterprise.Management.Business
{
    public class CategoryBLL : BaseBLL
    {
        public DP_Category GetCategory(int id)
        {
            return DataContext.DP_Category.First(m => m.Category_ID == id);
        }

        public List<DP_Category> GetCategories(int? parent)
        {
            if (parent.HasValue)
            {
                return DataContext.DP_Category.Where(m => m.Category_Parent == parent && m.Category_Status == 1).ToList();
            }
            else
            {
                return DataContext.DP_Category.Where(m => !m.Category_Parent.HasValue && m.Category_Status == 1).ToList();
            }
        }

        public List<DP_Category> GetBranchCategories(string name)
        {
            return (from c in DataContext.DP_Category
                    where c.Category_Level == (int)DeptCategoryLevelType.LevelIII
                    && SqlMethods.Like(c.Category_Name, "%" + name + "%")
                    && c.Category_Status == 1
                    orderby c.Category_Parent
                    select c).ToList();
        }

        public DP_Category GetParentCategory(int id)
        {
            return DataContext.DP_Category.First(m => m.Category_ID == id).DP_Category1;
        }


    }
}